/**
 * @(#) CongFamilyMediator.as
 *
 * @date		2009. 02. 25.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	가족정보 - Mediator
 * 				가족정보 Presentation Logic을 관리
 */
package com.skt.common_hr.congmoney_nos.view
{
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.congmoney_nos.CelebrationFacade;
	import com.skt.common_hr.congmoney_nos.events.FamilyEvent;
	import com.skt.common_hr.congmoney_nos.model.FamilyProxy;
	import com.skt.common_hr.congmoney_nos.model.vo.CongFamilyVO;
	import com.skt.common_hr.congmoney_nos.view.components.CongFamilyViewNos;

	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;

	import org.puremvc.as3.multicore.interfaces.INotification;


	public class FamilyMediator extends CommonMediator
	{
		public static const NAME:String = "CongFamilyMediator";

		private var prev_pernr:String = "";

		/**
		* 생성자
		*
		* @param
		* @return void
		*/
        public function FamilyMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }

		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
 			//setEventListeners();
 		}


 		private function setEventListeners():void
 		{
			view.addEventListener(FamilyEvent.FAMILY_INFO, onFamilyInfo);
 		}

 		private function removeEventListeners():void
 		{
			view.removeEventListener(FamilyEvent.FAMILY_INFO, onFamilyInfo);
 		}

 		override protected function clearView():void
 		{
 			trace("clear Work");
 		}

 		private var filter:String = "";

 		public function execute(pernr:String, filter:String=""):void
 		{
 			var proxy:FamilyProxy = facade.retrieveProxy(FamilyProxy.NAME) as FamilyProxy;

 			this.filter = filter;
 			if(pernr != prev_pernr || proxy.family==null) {
				proxy.load(pernr);
			}
			else {
				displayFamilyList();
			}
			prev_pernr = pernr;
			setEventListeners();
 		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		*
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return [
					CelebrationFacade.LOAD_FAMILY_COMPLETE,
					CelebrationFacade.LOAD_FAMILY_FAILED
					];
		}


		/**
		* Noti별로 처리한다.
		*
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName())
			{
				case CelebrationFacade.LOAD_FAMILY_COMPLETE:
					displayFamilyList();
					break;
				case CelebrationFacade.LOAD_FAMILY_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, "ALERT_FAILED_LOAD_FAMILY");
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}


		private function displayFamilyList():void
		{
			var proxy:FamilyProxy = facade.retrieveProxy(FamilyProxy.NAME) as FamilyProxy;
			view.writeDataList = proxy.family;
			if(filter.length>0) {
				view.writeDataList = familyFilter(proxy.family, filter);
			}
			else {
				view.writeDataList = proxy.family;
			}
			view.writeDataList.refresh();
		}

		private function familyFilter(src:ArrayCollection, filter:String):ArrayCollection
		{
			var arr:ArrayCollection = new ArrayCollection();
			for each(var item:CongFamilyVO in src) {
				if(item.FAMILY == filter) {
					arr.addItem(item);
				}
			}
			return arr;
		}

		private function onFamilyInfo(event:FamilyEvent):void
		{
			removeEventListeners();
			PopUpManager.removePopUp(view);

			facade.sendNotification(CelebrationFacade.SELECTED_FAMILY, event.family);
		}




		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():CongFamilyViewNos
        {
            return viewComponent as CongFamilyViewNos;
        }
	}
}